# -*- coding: utf-8 -*-

# 导入需要的包
import requests
import hashlib
import time
from dingtalkchatbot.chatbot import DingtalkChatbot
from datetime import datetime

# 指定梦网相关信息
url = 'http://61.145.229.28:7902/sms/v2/std/get_balance'
userid = ['JS8619', 'JS8620', 'JS9015', 'JS4855',
          'JS8528', 'JS8529', 'JS8595', 'JS8596']
pwd = ['868125', '868125', '256310', '265210',
       '171328', '171329', '456888', '456777']

# 获取时间戳
timestamp = time.strftime("%m%d%H%M%S", time.localtime())

# 定义输出空列表
result_uid = []
result_json = []
blance_list = []

# 设置 Webhook 地址
webhook = 'https://oapi.dingtalk.com/robot/send?access_token=c54fa6837d584b0abc56c0a7d0456ca37d59213dcc7ea3f6f104b20de3c71284'
secret = 'SEC9de9f15e7d9bc3be3c0c5f50adcf283ff26189940919486b9cdb0feda9ee45b1'
# 初始化机器人
bot = DingtalkChatbot(webhook=webhook, secret=secret)


# 获取uid和对应的json信息
def getBlance():
    for uid, upwd in zip(userid, pwd):
        # 明文密码 MD5 加密
        tempPwd = uid.upper() + '00000000' + upwd + timestamp
        md5 = hashlib.md5()
        md5.update(tempPwd.encode('utf-8'))
        md5Pwd = md5.hexdigest()

        # 定义字典
        data = {
            'userid': uid,
            'pwd': md5Pwd,
            'timestamp': timestamp,
        }

        # 获取 json
        response = requests.post(url, data=data)
        result = response.json()
        result_uid.append(uid)
        result_json.append(result)
    return result_uid, result_json


# 获取各个账号余量
def Blance():
    for uid, result in zip(result_uid, result_json):
        if result['result'] == 0:
            if uid == 'JS8619':
                sms_balance_8619 = int(result['balance'])
                blance_list.append(sms_balance_8619)
            if uid == 'JS8620':
                sms_balance_8620 = int(result['balance'])
                blance_list.append(sms_balance_8620)
            if uid == 'JS9015':
                sms_balance_9015 = int(result['balance'])
                blance_list.append(sms_balance_9015)
            if uid == 'JS4855':
                sms_balance_4855 = int(result['balance'])
                blance_list.append(sms_balance_4855)
            if uid == 'JS8528':
                sms_balance_8528 = int(result['balance'])
                blance_list.append(sms_balance_8528)
            if uid == 'JS8529':
                sms_balance_8529 = int(result['balance'])
                blance_list.append(sms_balance_8529)
            if uid == 'JS8595':
                sms_balance_8595 = int(result['balance'])
                blance_list.append(sms_balance_8595)
            if uid == 'JS8596':
                sms_balance_8596 = int(result['balance'])
                blance_list.append(sms_balance_8596)
        else:
            msg = '* ' + uid + ' 查询失败，错误码为：{}*'.format(result['result'])
            bot.send_text(msg)
    return blance_list


# 获取当前时间
def nowTime():
    now = datetime.now()

    # 将时间格式化为字符串
    formatted_time = now.strftime("%Y-%m-%d %H:%M:%S")

    return formatted_time


# 获取当前余量
old_time = nowTime()
old_result_uid, old_result_json = getBlance()
old = Blance()
result_uid = []
result_json = []
blance_list = []

print('{} old 值 {}'.format(old_time, old))

# 暂停10分钟
time.sleep(600)

# 获取当前余量
new_time = nowTime()
new_result_uid, new_result_json = getBlance()
new = Blance()
result_uid = []
result_json = []
blance_list = []

print('{} new 值 {}'.format(new_time, new))

# 开始做判断
if old[0]-new[0] < 200:
    nowtime = nowTime()
    print("{} JS8619 无异常。".format(nowtime))
    if new[0] < 30000:
        nowtime = nowTime()
        now_blance = new[0]
        msg = '*温馨提示：{} 新浪健康(生产账号) JS8619 '.format(nowtime) + '当前余量为 {}, '.format(now_blance) + \
            '少于 30000 条, 请关注! *'
        print(msg)
        bot.send_text(msg)
else:
    nowtime = nowTime()
    now_blance = new[0]
    send_sum_msg = old[0]-new[0]
    msg = '*温馨提示：{} 新浪健康(生产账号) JS8619 '.format(nowtime) + '当前余量为 {}, '.format(now_blance) + \
        '最近十分钟发送量为 {}, 超 200 条, 请关注! *'.format(send_sum_msg)
    print(msg)
    bot.send_text(msg)

if old[1]-new[1] < 200:
    nowtime = nowTime()
    print("{} JS8620 无异常。".format(nowtime))
    if new[1] < 30000:
        nowtime = nowTime()
        now_blance = new[1]
        msg = '*温馨提示：{} 新浪健康(服务账号) JS8620 '.format(nowtime) + '当前余量为 {}, '.format(now_blance) + \
            '少于 30000 条, 请关注! *'
        print(msg)
        bot.send_text(msg)
else:
    nowtime = nowTime()
    now_blance = new[1]
    send_sum_msg = old[1]-new[1]
    msg = '*温馨提示：{} 新浪健康(服务账号) JS8620 '.format(nowtime) + '当前余量为 {}, '.format(now_blance) + \
        '最近十分钟发送量为 {}, 超 200 条, 请关注! *'.format(send_sum_msg)
    print(msg)
    bot.send_text(msg)

if old[2]-new[2] < 200:
    nowtime = nowTime()
    print("{} JS9015 无异常。".format(nowtime))
    if new[2] < 10000:
        nowtime = nowTime()
        now_blance = new[2]
        msg = '*温馨提示：{} 爱问医生 JS9015 '.format(nowtime) + '当前余量为 {}, '.format(now_blance) + \
            '少于 10000 条, 请关注! *'
        print(msg)
        bot.send_text(msg)
else:
    nowtime = nowTime()
    now_blance = new[2]
    send_sum_msg = old[2]-new[2]
    msg = '*温馨提示：{} 爱问医生 JS9015 '.format(nowtime) + '当前余量为 {}, '.format(now_blance) + \
        '最近十分钟发送量为 {}, 超 200 条, 请关注! *'.format(send_sum_msg)
    print(msg)
    bot.send_text(msg)

if old[3]-new[3] < 200:
    nowtime = nowTime()
    print("{} JS4855 无异常。".format(nowtime))
    if new[3] < 10000:
        nowtime = nowTime()
        now_blance = new[3]
        msg = '*温馨提示：{} 营销类爱问医生 JS4855 '.format(nowtime) + '当前余量为 {}, '.format(now_blance) + \
            '少于 10000 条, 请关注! *'
        print(msg)
        bot.send_text(msg)
else:
    nowtime = nowTime()
    now_blance = new[3]
    send_sum_msg = old[3]-new[3]
    msg = '*温馨提示：{} 营销类爱问医生 JS4855 '.format(nowtime) + '当前余量为 {}, '.format(now_blance) + \
        '最近十分钟发送量为 {}, 超 200 条, 请关注! *'.format(send_sum_msg)
    print(msg)
    bot.send_text(msg)

if old[4]-new[4] < 200:
    nowtime = nowTime()
    print("{} JS8528 无异常。".format(nowtime))
    if new[4] < 10000:
        nowtime = nowTime()
        now_blance = new[4]
        msg = '*温馨提示：{} 爱问健康 JS8528 '.format(nowtime) + '当前余量为 {}, '.format(now_blance) + \
            '少于 10000 条, 请关注! *'
        print(msg)
        bot.send_text(msg)
else:
    nowtime = nowTime()
    now_blance = new[4]
    send_sum_msg = old[4]-new[4]
    msg = '*温馨提示：{} 爱问健康 JS8528 '.format(nowtime) + '当前余量为 {}, '.format(now_blance) + \
        '最近十分钟发送量为 {}, 超 200 条, 请关注! *'.format(send_sum_msg)
    print(msg)
    bot.send_text(msg)

if old[5]-new[5] < 200:
    nowtime = nowTime()
    print("{} JS8529 无异常。".format(nowtime))
    if new[5] < 10000:
        nowtime = nowTime()
        now_blance = new[5]
        msg = '*温馨提示：{} 营销类爱问健康 JS8529 '.format(nowtime) + '当前余量为 {}, '.format(now_blance) + \
            '少于 10000 条, 请关注! *'
        print(msg)
        bot.send_text(msg)
else:
    nowtime = nowTime()
    now_blance = new[5]
    send_sum_msg = old[5]-new[5]
    msg = '*温馨提示：{} 营销类爱问健康 JS8529 '.format(nowtime) + '当前余量为 {}, '.format(now_blance) + \
        '最近十分钟发送量为 {}, 超 200 条, 请关注! *'.format(send_sum_msg)
    print(msg)
    bot.send_text(msg)

if old[6]-new[6] < 200:
    nowtime = nowTime()
    print("{} JS8595 无异常。".format(nowtime))
    if new[6] < 10000:
        nowtime = nowTime()
        now_blance = new[6]
        msg = '*温馨提示：{} 爱问社区 JS8595 '.format(nowtime) + '当前余量为 {}, '.format(now_blance) + \
            '少于 10000 条, 请关注! *'
        print(msg)
        bot.send_text(msg)
else:
    nowtime = nowTime()
    now_blance = new[6]
    send_sum_msg = old[6]-new[6]
    msg = '*温馨提示：{} 爱问社区 JS8595 '.format(nowtime) + '当前余量为 {}, '.format(now_blance) + \
        '最近十分钟发送量为 {}, 超 200 条, 请关注! *'.format(send_sum_msg)
    print(msg)
    bot.send_text(msg)

if old[7]-new[7] < 200:
    nowtime = nowTime()
    print("{} JS8596 无异常。".format(nowtime))
    if new[7] < 10000:
        nowtime = nowTime()
        now_blance = new[7]
        msg = '*温馨提示：{} 营销类爱问社区 JS8596 '.format(nowtime) + '当前余量为 {}, '.format(now_blance) + \
            '少于 10000 条, 请关注! *'
        print(msg)
        bot.send_text(msg)
else:
    nowtime = nowTime()
    now_blance = new[7]
    send_sum_msg = old[7]-new[7]
    msg = '*温馨提示：{} 营销类爱问社区 JS8596 '.format(nowtime) + '当前余量为 {}, '.format(now_blance) + \
        '最近十分钟发送量为 {}, 超 200 条, 请关注! *'.format(send_sum_msg)
    print(msg)
    bot.send_text(msg)

